//
//  Category_product_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/6.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Category_product_vc.h"
#import "Category_product_CvCell.h"
#import "App_category.h"
#import "Product_add_one_tip_vc.h"

@interface Category_product_vc ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)CGFloat itemWidth;
@property (nonatomic, assign)CGFloat itemHeight;

@end

@implementation Category_product_vc


- (void)setXBParam:(NSArray *)XBParam{
 
    _XBParam = XBParam;
    
    DLog(@"%lu个", (unsigned long)XBParam.count);
    if (XBParam.count == 0) {
        [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];

    }else{
        [self hideEmptyView];
    }
    self.dataArr = XBParam.mutableCopy;
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
}
- (void)initSubviews{
    [super initSubviews];
    self.title = @"添加智能家具";
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat padding = 10.0f;
    flowLayout.minimumInteritemSpacing = 1 ; //两个元素之间的最小间距
    
    flowLayout.minimumLineSpacing =   padding ; //行间距 最小间距
    //        flowLayout.sectionInset = UIEdgeInsetsMake(padding * 0.1, padding ,padding  , padding ); //设置item的边距
    
    CGFloat w =  (ScreenWidth - padding * 2 ) / 4.0;
    self.itemWidth = w ;
    
    CGFloat h = 122 * self.itemWidth / 140.0;
    self.itemHeight = h + 40;
    
    
    self.collectionView.collectionViewLayout = flowLayout;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    // 注册CvCell
    [self.collectionView registerClass:[Category_product_CvCell class] forCellWithReuseIdentifier:@"Category_product_CvCell"];
    
}
#pragma mark ====== UICollectionViewDelegate ======
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Category_product_CvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category_product_CvCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[Category_product_CvCell alloc]init];
    }
    
    App_categoryProduct_list *aDevice = [self.dataArr objectAtIndex:indexPath.row];
    cell.entity = aDevice;
    
    return cell;
    
    
}

//跳转到详情页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    Product_add_one_tip_vc *vc= [[Product_add_one_tip_vc alloc]init];
    App_categoryProduct_list *aDevice = [self.dataArr objectAtIndex:indexPath.row];

    vc.aDevice = aDevice;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.itemWidth, self.itemHeight);
}

#pragma mark - 无数据占位

#pragma mark - DZNEmptyDataSetDelegate Methods
//
//
//- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
//{
////    [self req];
//}
////- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
////{
////
////
////    [self loadRefresh];
////}
//
//
//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
//{
//    return [UIImage imageNamed:@"notime_img"];
//
//}
//- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
//{
//    NSString *text = @"暂无数据";
//    UIFont *font = [UIFont boldSystemFontOfSize:15.0];
//    UIColor *textColor = themeColor;
//
//    NSMutableDictionary *attributes = [NSMutableDictionary new];
//
//
//    if (!text) {
//        return nil;
//    }
//
//    if (font) [attributes setObject:font forKey:NSFontAttributeName];
//    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
//
//    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
//
//}

@end
